/** Progetto PRAISED **/
package it.uniroma3.dia.praised.factory;

import java.io.IOException;
import java.net.URL;

import it.uniroma3.dia.praised.exception.FactoryException;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
 * WebClientFactory : 
 * 
 * @author Accosta Augusto
 * @author Lopez Elisabetta
 * @version 1.0
 *
 */
public class WebClientFactory {
	
	/**
	 * @return webClient
	 */
	public static WebClient newWebClient() {
		WebClient webClient = new WebClient();
		webClient.setCssEnabled(false);
		webClient.setJavaScriptEnabled(false);
		
		return webClient;
	}
	
	/**
	 * This method return a HtmlPage from an URL
	 * 
	 * @param url
	 * @return page
	 * @throws FactoryException
	 */
	public static HtmlPage newHtmlPage(URL url) throws FactoryException {
		WebClient webClient = new WebClient();
		webClient.setCssEnabled(false);
		webClient.setJavaScriptEnabled(false);
		
		WebRequest request = new WebRequest(url);
		request.setCharset("UTF-8");
		HtmlPage page = null;
		try {
			page = webClient.getPage(request);
		} catch (FailingHttpStatusCodeException e) {
			throw new FactoryException(e.getMessage());
		} catch (IOException e) {
			throw new FactoryException(e.getMessage());
		}
		
		return page;
	}
	
}
